package com.irdstudio.efp.nls.service.impl;

import com.irdstudio.basic.framework.core.util.DateTool;
import com.irdstudio.basic.framework.core.util.TimeUtil;
import com.irdstudio.cdp.pboc.service.facade.*;

import com.irdstudio.cdp.pboc.service.vo.PbocCreditBasicVO;
import com.irdstudio.cdp.pboc.service.vo.QueryReportVO;
import com.irdstudio.cdp.pboc.service.vo.ZXHtmlInfoVO;
import com.irdstudio.efp.esb.common.constant.ZXReturnType;
import com.irdstudio.efp.esb.service.bo.req.ZX10001QueryReports;
import com.irdstudio.efp.esb.service.facade.zx.ZXService;
import com.irdstudio.efp.esb.service.vo.zx.ZXVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 一代征信查询实现类: <br>
 * <p>
 * 0.1:caoxin:2019/5/9 11:11: TODO: <br>
 *
 * @author caoxin
 * @version 0.1
 * @date 2019/5/9 11:11
 * @since 2019/5/9 11:11
 */
@Service("generationCreditRulesService")
public class GenerationCreditQueryService {
    // 下标第一个元素
    private static int FRIST_INDEX = 0;
    /** 征信报告查询服务*/
    @Autowired
    @Qualifier("queryReportService")
    private QueryReportService queryReportService;
    /** 插入征信报告服务*/
    @Autowired
    @Qualifier("zxHtmlInfoService")
    private ZXHtmlInfoService zxHtmlInfoService;
    /** 卡中心征信JSON查询服务*/
    @Autowired
    @Qualifier("zx10001JSONService")
    private ZXService<ZX10001QueryReports> zxJSONService;
    /** 卡中心征信HTML查询服务*/
    @Autowired
    @Qualifier("zx10001HTMLService")
    private ZXService<ZX10001QueryReports> zxHTMLService;
    /** 贷记卡基本信息服务*/
    @Autowired
    @Qualifier("pbocCreditBasicService")
    private PbocCreditBasicService pbocCreditBasicService;
    /**
     * TODO: <br>
     *
     * 0.1:caoxin:2019/5/9 15:35: <br>
     * @author caoxin
     * @return
     * @since 2019/5/9 15:35
     * @date 2019/5/9 15:35
     * @version 0.1
     */

    boolean creditRuleOfOneHasCreditReport(String reportId){
        return Boolean.TRUE;
    }
    /**
     * 征信查询并且入库返回征信报告ReportId: <br>
     * 0.1:caoxin:2019/5/9 11:33: <br>
     * @author caoxin
     * @return
     * @since 2019/5/9 11:33
     * @date 2019/5/9 11:33
     * @version 0.1
     */

    public String queryGenerationCredit(String cusName,String certCode,String queryType,String queryReason,String applyDate,String lmtApplySeq,String proCode,String zxOpId) throws Exception {
        //// 获取最近一次征信查询报告数据
        //QueryReportVO creditReport = isCreditReportExist(getCreditReports(certCode));
        //// 存在判断是否是否在30天内
        //if (creditReport != null){
        //    if (isCreditReportOver30days(creditReport)){
        //        reportId = insertGenerationCreditReporst(cusName,certCode,queryType,queryReason,applyDate,lmtApplySeq,proCode,zxOpId);
        //    }else {
        //        // 返回最新的30天内的reportId
        //        reportId = creditReport.getReportId();
        //    }
        //}else { // 不存在直接查询征信
        //    reportId =
        //}
        return  insertGenerationCreditReporst(cusName,certCode,queryType,queryReason,applyDate,lmtApplySeq,proCode,zxOpId);
    }

    /**
     * 查询贷记卡基本信息: <br>
     * 0.1:caoxin:2019/5/9 19:33: <br>
     * @author caoxin
     * @return
     * @since 2019/5/9 19:33
     * @date 2019/5/9 19:33
     * @version 0.1
     */
    public List<PbocCreditBasicVO> queryCreditBasicInfo(String reportId) throws Exception {
        PbocCreditBasicVO pbocCreditBasicVO = new PbocCreditBasicVO();
        pbocCreditBasicVO.setFId(reportId);
        return Optional.ofNullable(pbocCreditBasicService.selectByReportId(pbocCreditBasicVO)).orElseGet(ArrayList::new);
    }

    /**
     * 调用单笔征信接口并 插入征信报告: <br>
     * 0.1:caoxin:2019/5/9 14:37: TODO: <br>
     * @author caoxin
     * @return
     * @since 2019/5/9 14:37
     * @date 2019/5/9 14:37
     * @version 0.1
     */

    private String insertGenerationCreditReporst(String cusName,String certCode,String queryType,String queryReason,String applyDate,String lmtApplySeq,String proCode,String zxOpId) throws Exception {
        // 调用征信JSON接口
        ZX10001QueryReports zxJsonQueryReports = buildGenerationCreditJSONQueryReports(cusName, certCode, queryType, queryReason, applyDate, lmtApplySeq, proCode);
        ZXVO zxvoJson = zxJSONService.service(zxJsonQueryReports,zxOpId);
        // 调用征信HTML接口
        ZX10001QueryReports zxHtmlQueryReports = buildGenerationCreditHTMLQueryReports(cusName, certCode, queryType, queryReason, applyDate, lmtApplySeq, proCode);
        ZXVO zxvoHtml = zxHTMLService.service(zxHtmlQueryReports,zxOpId);
        // 获取征信报告ID
        String reportId = (String) zxvoJson.getResp();
        ZXHtmlInfoVO zxHtmlInfoVO = (ZXHtmlInfoVO) zxvoHtml.getResp();
        zxHtmlInfoVO.setReportId(reportId);
        zxHtmlInfoService.insert(zxHtmlInfoVO);
        return reportId;
    }
    /**
     * 通过客户ID获取征信报告: <br>
     * 0.1:caoxin:2019/5/9 10:48: <br>
     * @author caoxin
     * @return
     * @since 2019/5/9 10:48
     * @date 2019/5/9 10:48
     * @version 0.1
     */
    private List<QueryReportVO> getCreditReports(String certCode) throws Exception {
        QueryReportVO queryReportVo = new QueryReportVO();
        queryReportVo.setCercode(certCode);
        return queryReportService.queryReportId(queryReportVo);
    }

    /**
     * 判断征信报告是否有数据，有则返回最新一条，通过数据库日期排序控制，无则返回空: <br>
     * 0.1:caoxin:2019/5/9 11:19: TODO: <br>
     * @author caoxin
     * @return
     * @since 2019/5/9 11:19
     * @date 2019/5/9 11:19
     * @version 0.1
     */
    public static QueryReportVO isCreditReportExist(List<QueryReportVO> queryReportVOList){
        return !queryReportVOList.isEmpty() ? queryReportVOList.get(FRIST_INDEX):null;
    }
    /**
     * 判断征信报告是否在30天: <br>
     * 0.1:caoxin:2019/5/9 11:26: TODO: <br>
     * @author caoxin
     * @return
     * @since 2019/5/9 11:26
     * @date 2019/5/9 11:26
     * @version 0.1
     */
    public static boolean isCreditReportOver30days(QueryReportVO queryReportVO) throws Exception {
        //最新征信报告时间 时间格式为：yyyy.MM.DD
        String reportTime = queryReportVO.getReportTime();
        //转换yyyy-MM-dd
        reportTime = StringUtils.replace(reportTime, ".", "-");
        // 判断是否超过30天
        int isOver = TimeUtil.compareDates(TimeUtil.addDay(reportTime, 30), DateTool.getCurrentDateTime());
        return isOver < 0;
    }
    /**
     * 一代征信查询JSON格式的请求报文: <br>
     * 0.1:caoxin:2019/5/9 11:02: <br>
     * @author caoxin
     * @return
     * @since 2019/5/9 11:02
     * @date 2019/5/9 11:02
     * @version 0.1
     */
    private ZX10001QueryReports buildGenerationCreditJSONQueryReports(String cusName, String certCode, String queryType, String queryReason, String applyDate, String lmtApplySeq, String proCode){
        return  new ZX10001QueryReports.Builder().withCustNm(cusName).withCertNo(certCode).withQryTp(queryType)
                .withQryRsn(queryReason)
                .withRtnType(ZXReturnType.JSON.VALUE).withAuthCd(lmtApplySeq).withBizType(proCode).withAuthTm(applyDate).build();
    }
    /**
     * 一代征信查询HTML格式的请求报文: <br>
     * 0.1:caoxin:2019/5/9 11:02: <br>
     * @author caoxin
     * @return
     * @since 2019/5/9 11:02
     * @date 2019/5/9 11:02
     * @version 0.1
     */
    private ZX10001QueryReports buildGenerationCreditHTMLQueryReports(String cusName,String certCode,String queryType,String queryReason,String applyDate,String lmtApplySeq,String proCode){
        return  new ZX10001QueryReports.Builder().withCustNm(cusName).withCertNo(certCode).withQryTp(queryType)
                .withQryRsn(queryReason)
                .withRtnType(ZXReturnType.HTML.VALUE).withAuthCd(lmtApplySeq).withBizType(proCode).withAuthTm(applyDate).build();
    }

}
